#pragma once

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <unordered_set>
#include <string>
#include "unistd.h"

#include "Log.hpp"
#include "InetAddr.hpp"
#include "ConnectMysql.hpp"

using namespace log_ns;

class Command
{
private:
    ConnectMysql my;
public:
    Command()
    {
        
    }
    std::string Execute(const char *cmd)
    {
        // std::cout << cmd << std::endl;
        // 执行mysql命令
        return my.Execute(cmd);
    }

    void HandlerCommand(int sockfd, InetAddr &addr)
    {
        while (1)
        {
            char buffer[1024];
            int n = read(sockfd, buffer, sizeof(buffer) - 1);
            if (n > 0)
            {
                buffer[n] = 0;
                std::string echo = Execute(buffer);
                std::cout << echo << std::endl;
                write(sockfd, echo.c_str(), echo.size());
                LOG(INFO, "echo success\n");
            }
            else if (n == 0)
            {
                LOG(INFO, "client %s quit\n", addr.AddrStr().c_str());
                break;
            }
            else
            {
                LOG(ERROR, "read error\n");
                break;
            }
        }
    }

    ~Command()
    {
    }

private:
    
};
